/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using ESLReminder;

namespace LucasCode
{
    public class ConsoleParserHL1 : ConsoleParserBase
    {
        private List<MatchPlayer> m_Players = new List<MatchPlayer>();

        public MatchPlayer[] Players
        {
            get
            {
                return m_Players.ToArray();
            }
        }

        private MatchPlayer GetPlayer(List<MatchPlayer> players, string name)
        {
            foreach (MatchPlayer p in players)
            {
                if (p.CurrentNick == name)
                {
                    return p;
                }
            }
            MatchPlayer pl = new MatchPlayer(name);
            players.Add(pl);
            return pl;
        }

        public override void Parse(ESLReminder.ConsoleEventItemCollection ConsoleEventList)
        {
            m_Players.Clear();
            string UserRegex = @"#\s*\d+\s+""(.+?)""\s+\d+\s+STEAM_(\d+:\d+:\d+)";
            MatchPlayer Killer = null, Killee = null;
            int i = 0;
            foreach (ConsoleEventItem ev in ConsoleEventList)
            {
                foreach (Match m in Regex.Matches(ev.EventText, UserRegex))
                {
                    MatchPlayer p = GetPlayer(m_Players, m.Groups[1].Value);
                    string gidtype = string.Empty;
                    p.GameID = m.Groups[2].Value;
                    p.GameAccountList.Add(new GameAccount("SteamID", m.Groups[2].Value));
                }

                Match NameRegex = Regex.Match(ev.EventText, @"^\* (.*?) changed name to (.*?)$");
                if (NameRegex.Success)
                    GetPlayer(m_Players, NameRegex.Groups[1].Value).CurrentNick = NameRegex.Groups[2].Value;
                else
                {
                    switch (ev.EventText)
                    {
                        case "killed":
                            Killer = GetPlayer(m_Players, ConsoleEventList[i - 1].EventText);
                            Killee = GetPlayer(m_Players, ConsoleEventList[i + 1].EventText);

                            bool headshot = false;
                            if (ConsoleEventList[i + 2].EventText == "with a headshot from")
                            {
                                headshot = true;
                            }
                            Killer.AddAction(new MatchAction(MatchActions.Kills, Killer, Killee, MatchWeapons.None, headshot));
                            Killee.AddAction(new MatchAction(MatchActions.Dies, Killee, Killer, MatchWeapons.None, headshot));
                            break;
                    }
                }
                i++;
            }
        }
    }
}
